let express = require("express")();
let expressStatic = require("express-static");
let mysql = require("mysql");
let port = 81;

//规划一个数据库
let sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'news'
});

//尝试连接数据库
sql.connect();

//接收前端列表页的请求
express.get("/getNewsList", (request, response) => {
    //查库
    sql.query(`SELECT id,img,title,txt FROM news`, (error, data) => {
        response.send(data);
    })
})

//接收前端详情页的请求
express.get("/getNews", (request, response) => {
    let id = request.query.id;
    console.log(id)
    sql.query(`SELECT * FROM news WHERE id="${id}"`, (error, data) => {
        if (error) {
            response.send("error");
            console.log(error)
        } else {
            response.send(data[0])
        }
    })
})


//接收到来自前端的静态页面请求
express.use(expressStatic(__dirname + "/chuanyue"));
console.log(`running at ${port}`)
express.listen(port);